package jdbc;

import javax.activation.CommandInfo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 用户登录
 * 程序启动后要求用户输入用户名和密码，
 * 然后比对userinfo表中该用户是否为管理员，如果匹配成则提示成功
 * 否则提示：用户名或密码不正确
 * 登录逻辑：只要用户名密码都正确才算登录成功
 * */
public class loginDemo {
    public static void main(String[] args) {

        UserInfo UserInfo = InputUtil.getInputObject(new UserInfo(), "用户登录", "DEFAULT");
        try (
                Connection connection = DBUtil.getConnection();
        ){
            Statement statement = connection.createStatement();
            String sql="SELECT id, username, password, nickname, age " +
                    "FROM userinfo " +
                    "WHERE username='"+UserInfo.getUsername()+"' AND password='"+UserInfo.getPassword()+"'";
            ResultSet rs = statement.executeQuery(sql);
            if (rs.next()) {
            System.out.println("登录成功,欢迎您："+rs.getString("nickname"));
        } else {
            System.out.println("登录失败,用户明或者密码不正确");
        }

    } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }}
